<template>
  <PageWrapper>
    <template #extra>
      <el-button type="primary" @click="handleCreate">新增</el-button>
    </template>
    <template #title>
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item>
          <el-input v-model="formInline.user" placeholder="请输入用户名称" clearable />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery">查找</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </template>
    <template #content>
      <el-table :data="pager.lists"  style="width: 100%" border>
        <el-table-column prop="id" label="id" width="180" />
        <el-table-column prop="name" label="姓名" width="180" />
        <el-table-column fixed="right" label="Operations" width="120">
          <template #default="scope">
            <el-button link type="primary" size="small" @click="handleEdit(scope.row)">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="page-customer">
        <el-pagination background layout="prev, pager, next" :page-size="pager.pageSize" :total="pager.total" @change="changePage"  />
      </div>
    </template>
  </PageWrapper>

  <el-dialog v-model="dialogVisible" title="新增/编辑" width="500" draggable>
    <el-form
        ref="ruleFormRef"
        style="max-width: 600px"
        :model="userForm"
        status-icon
        label-width="auto"
        class="demo-ruleForm"
    >
      <el-form-item label="名称" prop="name">
        <el-input v-model="userForm.name" autocomplete="off" />
      </el-form-item>
      <el-form-item label="帐号" prop="account">
        <el-input v-model="userForm.account" autocomplete="off" :disabled="isUpdated" />
      </el-form-item>
      <el-form-item label="密码" prop="account" v-show="!isUpdated">
        <el-input type="password" v-model="userForm.password" autocomplete="off"  />
      </el-form-item>
      <el-form-item label="角色">
        <el-select
            v-model="userForm.roleIds"
            multiple
            placeholder="Select"
            style="width: 240px"
        >
          <el-option
              v-for="item in selectRoleList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handelSubmit">
          提交
        </el-button>
      </div>
    </template>
  </el-dialog>

</template>

<script setup lang="jsx">
import PageWrapper from "@/components/pageWrapper/index.vue";
import {reactive, ref} from "vue";
import {selectRole} from "@/api/role.js";
import {createUser, getList, updateUser, detail} from "@/api/user.js";
import {usePaging} from "@/hooks/usePaging";
import {ElMessage} from "element-plus";

const dialogVisible = ref(false)
let userForm = reactive({
  id:'',
  name: '',
  account:'',
  roleIds:''
})
const selectRoleList = ref([])
const formInline = reactive({})
const clear = () => {
  userForm= reactive({})
  isUpdated.value = false
}
const getRoleList = async () => {
  selectRoleList.value = await selectRole()
}
const handleCreate = () => {
  clear()
  getRoleList()
  dialogVisible.value = true
}
const isUpdated = ref(false)
const handleEdit = async(row) => {
  clear()
  getRoleList()
  const res = await detail({id:row.id})
  isUpdated.value = true
  Object.assign(userForm, {id:res.id, name: res.name, account: res.account, roleIds: res.roleIds})
  dialogVisible.value = true
}

const {pager, changePage, getListFn, handleQuery} = usePaging({
  fn:getList,
  initParams:formInline
})

/** 查询 */

/** 重置查询 */
function resetQuery() {
  // queryFormRef.value.resetFields();
  // queryParams.pageNum = 1;
  // handleQuery();
}
const handleClose = () => {
  clear()
  dialogVisible.value = false
}
const handelSubmit = async () => {
  userForm.roleIds = userForm.roleIds.join(',')
  const api = userForm.id !== '' && userForm.id !== undefined ? updateUser : createUser
  const res = await api({...userForm})
  if (res.code === '000') {
    getListFn();
    handleClose()
    ElMessage({
      message: '新增成功',
      type: 'success',
    })
  }
}

getListFn()
</script>

<style scoped>

</style>